package com.example.demo01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author wangxun
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private static final String PASSWORD = "123456";
    private static final String LOGIN_USER = "loginUser";

    @PostMapping(path = "/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, String> map, HttpSession session) {

        if (!StringUtils.isEmpty(username) && PASSWORD.equals(password)) {
            session.setAttribute(LOGIN_USER, username);
            return "redirect:/main.html";
        } else {
            map.put("msg", "用户名密码错误");
            return "login";
        }
    }
}
